package 序列化;

import jdk.internal.org.objectweb.asm.tree.MultiANewArrayInsnNode;

import java.io.*;

class point implements Serializable{
    private static final long serialVersionUID = 309550399391666178L;
    int x;
    transient int y;//加了transient不进行序列化和反序列化
    int z;

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    @Override
    public String toString() {
        return "point{" +
                "x=" + x +
                '}';
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        System.out.println("WriteObject");
        x=x+2;//加密
        out.defaultWriteObject();  //序列化
    }
    private  void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        System.out.println("readObject");
        in.defaultReadObject();//反序列化
        x=x-2;//解密
    }
}
public class TestSerializable01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        point point=new point();
        point.setX(10);
        //序列化
        ObjectOutputStream out=new ObjectOutputStream(
                new FileOutputStream("a1.txt"));
        out.writeObject(point);
        out.close();
        //反序列化
        ObjectInputStream in=new ObjectInputStream(
                new FileInputStream("a1.txt"));
        Object o=in.readObject();
        System.out.println(o);
        in.close();
    }


}
